﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;

using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using KenLib.BusinessManager;
using KenLib.BusinessObject;
using System.Collections.Generic;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;


public partial class JOINT_frmJ_TraineeResumeOutput : System.Web.UI.Page
{
    ReportDocument repDoc = new ReportDocument();
    protected void Page_Load(object sender, EventArgs e)
    {
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("frmJ_TraineeResumeOutput");
        ((JOINT_MstPage_JOINT)this.Master).changeTitle("履歴書出力");
        if (Session["役"] == null || Session["役"].ToString() == "")
        {
            Response.Redirect("~/frmD_Login.aspx");
        }
        if (!IsPostBack)
        {
            ddl_組合選択.Focus();
            this.bindddl_組合選択();
            ViewState["pageIndex"] = 0;//gan chi so trang ban dau trong gridview=0
        }
        AppCode_Common.ShowEmtyGrid<応募情報>(grv, 5, "データーがありません。", "");

        RequiredFieldValidator1.ErrorMessage = GetError("ER00007");
        RequiredFieldValidator2.ErrorMessage = GetError("ER00007");
        RequiredFieldValidator3.ErrorMessage = GetError("ER00007");
    }

    public byte[] GetImageFromURL(string fileName)// phuong thuc nay doc mang len mang byte du lieu hinh anh
    {

        //Method to load an image from disk and return it as a bytestream

        System.IO.FileStream fs = new FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);

        System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
        byte[] result = br.ReadBytes(Convert.ToInt32(br.BaseStream.Length));
        fs.Close();
        br.Close();
        return result;
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "startup();", true);
    }

    protected void btnPDF出力_Click(object sender, EventArgs e)
    {
        String dateFileName = "";
        string filePath = "";
        try
        {
            DataSet ds = new DataSet();
            string repFilePath = Server.MapPath("~/Report/CrystalReport.rpt");

            repDoc.Load(repFilePath);


            応募学歴情報Manager manager1 = new 応募学歴情報Manager();

            DataTable t1 = new DataTable();
            t1 = manager1.selectAll応募学歴情報_luclt();
            t1.TableName = "応募学歴情報";

            DataTable t = (DataTable)ViewState["table"];
            t.TableName = "応募情報";

            AddImageColumn(t, "Picture");// them cot hien thi hinh anh
            for (int i = 0; i < t.Rows.Count; i++)
            {
                string filename = t.Rows[i]["写真"].ToString();
                if (filename == "")
                {
                    filePath = Server.MapPath("~/Images/Trainee/no-image.jpg");
                }
                else if (filename != "")
                {
                    string imageURL = "/Images/Joint_Staff/" + filename;
                    if (File.Exists(HttpContext.Current.Request.PhysicalApplicationPath + imageURL))
                    {
                        filePath = Server.MapPath("~/Images/Joint_Staff/" + filename);
                    }
                    else
                    {
                        filePath = Server.MapPath("~/Images/Trainee/no-image.jpg");
                    }
                }

                t.Rows[i]["Picture"] = LoadImage("Picture", filePath);

            }


            ds.Tables.Add(t);
            ds.Tables.Add(t1);
            //repDoc.Subreports[0].SetDataSource(ds);//  gan  datasource cho main report
            repDoc.SetDataSource(ds); //gan  datasource cho sub report
            repDoc.SetParameterValue("企業名", t.Rows[0]["企業名"].ToString());
            repDoc.SetParameterValue("主要製品", t.Rows[0]["主要製品"].ToString());
            repDoc.SetParameterValue("面接日", t.Rows[0]["面接日"].ToString());
            repDoc.SetParameterValue("組合名", t.Rows[0]["組合名"].ToString());
            repDoc.SetParameterValue("面接番号", t.Rows[0]["面接番号"].ToString());
            //repDoc.ParameterFields.Add(t.Rows[0][""]);
            try
            {
                // lay ra ten hiep hoi cong ty ,ngay phong van de gan vao ten file pdf xuat ra
                if (ddl_textbox2.SelectedValue.ToString() != "-1")
                {
                    DateTime d1;
                    d1 = Convert.ToDateTime(ddl_textbox2.SelectedValue);
                    string month = "" + d1.Month;
                    if (d1.Month < 10)
                    {
                        month = "0" + d1.Month;
                    }
                    string day = "" + d1.Day;
                    if (d1.Day < 10)
                    {
                        day = "0" + d1.Day;

                    }
                    dateFileName = d1.Year + month + day;
                }

            }
            catch (Exception Exception)
            {
                throw Exception;
            }
            string tenHiepHoi = "";
            string tenCongTy = "";
            if (Session["tenHiepHoi"] != null)
                tenHiepHoi = Session["tenHiepHoi"].ToString();

            if (Session["tenCongTy"] != null)
                tenCongTy = Session["tenCongTy"].ToString();
            string fileName = tenHiepHoi + tenCongTy + dateFileName;
            repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, fileName);
            repDoc.Close();
            repDoc.Dispose();

        }

        catch //(Exception ex)
        {
            //throw ex;
        }
        finally
        {

            repDoc.Close();

            repDoc.Dispose();
        }
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "startup();", true);

    }

    private void AddImageColumn(DataTable objDataTable, string strFieldName)// phuong thuc add them image 
    {
        try
        {
            DataColumn objDataColumn = new DataColumn(strFieldName, Type.GetType("System.Byte[]"));
            objDataTable.Columns.Add(objDataColumn);
        }
        catch (Exception ex)
        {

        }
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "startup();", true);
    }
    private byte[] LoadImage(string strImageField, string FilePath)
    {
        try
        {
            FileStream fs = new FileStream(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            byte[] Image = new byte[fs.Length];
            fs.Read(Image, 0, Convert.ToInt32(fs.Length));
            fs.Close();
            return Image;
        }

        catch (Exception ex)
        {
            return null;

        }
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "startup();", true);
    }
    protected void btn_検索_Click(object sender, EventArgs e)
    {
        応募情報Manager manager = new 応募情報Manager();
        本社情報Manager frm = new 本社情報Manager();
        応募情報 obj = new 応募情報();
        if (ddl_textbox2.ToString() != "")
        {
            DateTime d1;
            d1 = Convert.ToDateTime(ddl_textbox2.Text);
            obj.面接日 = d1;

        }
        if (ddl_組合選択.SelectedValue.ToString() != "-1")
        {
            obj.団体ID = Convert.ToInt32(ddl_組合選択.SelectedValue.ToString());
        }
        if (ddl_組合員企業選択.SelectedValue.ToString() != "-1")
        {
            obj.企業ID = Convert.ToInt32(ddl_組合員企業選択.SelectedValue.ToString());
        }
        grv.Visible = true;
        DataTable table = manager.frmJ_TraineeResumeOutput_Search(obj);
        //  DataColumn colum = new DataColumn("Gioitinh", typeof(string));
        DataColumn objDataColumn = new DataColumn("Gioitinh", typeof(string));
        table.Columns.Add(objDataColumn);
        foreach (DataRow row in table.Rows)
        {
            Boolean sex = Convert.ToBoolean(row["性別"].ToString());
            string result = "";

            if (sex == false)
            {
                result = "男";
                row["Gioitinh"] = result;
            }
            if (sex == true)
            {
                result = "女";
                row["Gioitinh"] = result;
            }
            string 組合員企業選択 = frm.getNamereport_企業名英語_luclt(ddl_組合員企業選択.SelectedIndex).ToString();
            string 組合選択 = frm.getNamereport_企業名英語_luclt(ddl_組合選択.SelectedIndex).ToString();
            Session.Add("tenHiepHoi", 組合選択);// gan ten hiep hoi de xuong fan report xuat ra ten report
            Session.Add("tenCongTy", 組合員企業選択);// gan ten cong ty de xuong fan report xuat ra ten report
            //Session.Add("tenHiepHoi", row["企業名英語"]);// gan ten hiep hoi de xuong fan report xuat ra ten report
            //Session.Add("tenCongTy", row["企業名英語1"]);// gan ten cong ty de xuong fan report xuat ra ten report

        }
        ViewState["table"] = table;
        showGridview(table);
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "startup();", true);
    }

    public void showGridview(DataTable table)
    {
        if (table.Rows.Count > 0)
        {
            grv.DataSource = table;
            grv.DataBind();
        }
        else if (table.Rows.Count <= 0)
        {
            AppCode_Common.ShowEmtyGrid<応募情報>(grv, 0, "データがありません。", "");
        }
    }

    protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)// phan trang.
    {
        grv.PageIndex = e.NewPageIndex;
        ViewState["pageIndex"] = e.NewPageIndex;
        int i = grv.Rows.Count;
        showGridview((DataTable)ViewState["table"]);
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "startup();", true);
    }
    public void bindddl_組合選択()// 
    {
        try
        {
            本社情報Manager frm = new 本社情報Manager();
            DataTable dt = frm.frmJ_TraineeResumeOutput_Get組合選択();
            ddl_組合選択.DataSource = dt;
            ddl_組合選択.DataTextField = "企業名";
            ddl_組合選択.DataValueField = "企業ID";
            ddl_組合選択.DataBind();
            System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
            item.Selected = true;
            ddl_組合選択.Items.Insert(0, item);
        }
        catch { }
    }

    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
        {
            return error = dt.Rows[0][2].ToString();
        }
        else
        {
            return error;
        }
    }
    protected void ddl_組合選択_SelectedIndexChanged(object sender, EventArgs e)// phuong thuc nay su ly load ddl ten cong ty phu thuoc vao ten hiep hoi
    {
        if (ddl_組合選択.SelectedValue.ToString() != "-1")
        {
            int 組合選択ID = Convert.ToInt32(ddl_組合選択.SelectedValue.ToString());
            本社情報Manager frm = new 本社情報Manager();
            DataTable dt = frm.sp_frmJ_TraineeSearch_Select_dll組合員企業選択_Dong(組合選択ID);
            ddl_組合員企業選択.DataSource = dt;
            ddl_組合員企業選択.DataTextField = "企業名";
            ddl_組合員企業選択.DataValueField = "企業ID";
            ddl_組合員企業選択.DataBind();
            System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
            item.Selected = true;
            ddl_組合員企業選択.Items.Insert(0, item);
            ddl_組合員企業選択.Focus();
        }
        else
        {
            ddl_組合員企業選択.Items.Clear();
            System.Web.UI.WebControls.ListItem 選択 = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
            ddl_組合員企業選択.Items.Insert(0, 選択);
            ddl_textbox2.Items.Clear();
            ddl_textbox2.Items.Insert(0, 選択);
            ddl_組合選択.Focus();
            HiddenField2.Value = "";
        }
        Page.Title = "履歴書出力";
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "startup();", true);
        RequiredFieldValidator1.Validate();

    }
    protected void ddl_組合員企業選択_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_組合員企業選択.SelectedValue.ToString() != "-1")
        {
            int 団体ID = Convert.ToInt32(ddl_組合選択.SelectedValue.ToString());
            int 企業ID = Convert.ToInt32(ddl_組合員企業選択.SelectedValue.ToString());
            面接情報Manager frm = new 面接情報Manager();
            DataTable dt = frm.frmJ_InterviewResultSearch_面接日(団体ID, 企業ID);
            if (dt.Rows.Count>0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    HiddenField2.Value += row["面接日"].ToString() + ",";
                }
            }
            ddl_textbox2.DataSource = dt;
            ddl_textbox2.DataTextField = "面接日";
            ddl_textbox2.DataValueField = "面接日";
            ddl_textbox2.DataBind();
            System.Web.UI.WebControls.ListItem item = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
            item.Selected = true;
            ddl_textbox2.Items.Insert(0, item);
            ddl_textbox2.Focus();
        }
        else
        {
            ddl_textbox2.Items.Clear();
            System.Web.UI.WebControls.ListItem 選択 = new System.Web.UI.WebControls.ListItem("[選択]", "-1");
            ddl_textbox2.Items.Insert(0, 選択);
            ddl_組合員企業選択.Focus();
        }
        Page.Title = "履歴書出力";

        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "startup();", true);
    }
    protected void ddl_textbox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        応募情報Manager manager = new 応募情報Manager();
        応募情報 obj = new 応募情報();
        本社情報Manager frm = new 本社情報Manager();
        if (ddl_textbox2.SelectedValue.ToString() != "-1")
        {
            DateTime d1;
            d1 = Convert.ToDateTime(ddl_textbox2.Text);
            obj.面接日 = d1;
        }
        obj.団体ID = Convert.ToInt32(ddl_組合選択.SelectedValue.ToString());
        obj.企業ID = Convert.ToInt32(ddl_組合員企業選択.SelectedValue.ToString());
        DataTable table = manager.frmJ_TraineeResumeOutput_Search(obj);
        DataColumn objDataColumn = new DataColumn("Gioitinh", typeof(string));
        table.Columns.Add(objDataColumn);
        foreach (DataRow row in table.Rows)
        {
            Boolean sex = Convert.ToBoolean(row["性別"].ToString());
            string result = "";

            if (sex == false)
            {
                result = "男";
                row["Gioitinh"] = result;
            }
            if (sex == true)
            {
                result = "女";
                row["Gioitinh"] = result;
            }
        }
            string 組合員企業選択 = frm.getNamereport_企業名英語_luclt(ddl_組合員企業選択.SelectedIndex).ToString();
            string 組合選択 = frm.getNamereport_企業名英語_luclt(ddl_組合選択.SelectedIndex).ToString();
            Session.Add("tenHiepHoi", 組合選択);// gan ten hiep hoi de xuong fan report xuat ra ten report
            Session.Add("tenCongTy", 組合員企業選択);// gan ten cong ty de xuong fan report xuat ra ten report
            ViewState["table"] = table;
            showGridview(table);
            ddl_textbox2.Focus();
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "regid", "startup();", true);
            
    }
}
